package com.huitone.gddw.service.impl.worktable;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.common.ComConstant;
import com.huitone.gddw.dao.entity.PriAlloc;
import com.huitone.gddw.dao.worktable.paramconfigmanage.IPriAllocDao;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IParamtabRecService;
import com.huitone.gddw.service.IPriAllocService;
import com.huitone.gddw.service.dto.PriAllocDTO;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 短信优先级分配 接口实现类
 * @author wuluodan
 *
 */
@Service
public class PriAllocService implements IPriAllocService {

	@Autowired
	private IPriAllocDao priAllocDao;
	
	@Autowired
	private IParamtabRecService paramtabRecService;
	
	@Override
	public QueryPage<PriAllocDTO> selectPage(QueryPage<PriAlloc> page) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("ruleId", page.getParams().get("ruleId"));
		map.put("svrNode", page.getParams().get("svrNode"));
		map.put("serviceCode", page.getParams().get("serviceCode"));
		map.put("isLevel", page.getParams().get("isLevel"));
		map.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		map.put("end", page.getPageNo() * page.getPageSize());
		List<PriAllocDTO> result = priAllocDao.selectPage(map);
		int totalCount = priAllocDao.selectCount(map);
		
		QueryPage<PriAllocDTO> queryPage = new QueryPage<PriAllocDTO>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}

	@Override
	public PriAlloc selectByPrimaryKey(String recId) {
		return priAllocDao.selectByPrimaryKey(recId);
	}

	@Override
	public PriAllocDTO toUpdate(String recId) {
		return new PriAllocDTO(this.selectByPrimaryKey(recId));
	}

	@Override
	public void update(PriAlloc entity) {
		entity.setLastModTime(new Date());
		entity.setLastModStaffId(SysLoginUtil.getLoginUserId());
		priAllocDao.updateByPrimaryKey(entity);
		
		this.updateParamRec("更新优先级分配");
	}

	@Override
	public void save(PriAlloc entity) {
		entity.setInsertTime(new Date());
		entity.setInsertStaffId(SysLoginUtil.getLoginUserId());
		entity.setLastModTime(entity.getInsertTime());
		entity.setLastModStaffId(entity.getInsertStaffId());
		priAllocDao.insertSelective(entity);
		
		this.updateParamRec("保存优先级分配");
	}

	@Override
	public void delete(String recId) {
		priAllocDao.deleteByPrimaryKey(recId);
		this.updateParamRec("删除优先级分配");
	}

	@Override
	public int deleteByCode(String svrNode, String serviceCode) {
		this.updateParamRec("根据业务端编码和短信类型编码删除优先级分配信息");
		return priAllocDao.delete(svrNode, serviceCode);
	}

	@Override
	public List<PriAlloc> getByRuleId(String ruleId) {
		return priAllocDao.getByRuleId(ruleId);
	}
	
	/**
	 * 更新参数记录表
	 * @param desc 说明
	 */
	private void updateParamRec(String desc) {
		paramtabRecService.update(ComConstant.PRI_ALLOC, desc);
	}

}
